
/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 小峰
 * Date: 2019-10-24
 * Time: 9:19
 */
public class ArrayList<T> extends AbstracList<T> {
    private T[] array;
    private static final int DEFAULT_CAPACITY = 10;


    public ArrayList(int capacity) {
        capacity = capacity < DEFAULT_CAPACITY ? DEFAULT_CAPACITY : capacity;
        array = (T[]) new Object[capacity];
    }

    public ArrayList() {
        this(DEFAULT_CAPACITY);
    }

    /**
     * 清除所有元素
     */
    @Override
    public void clear() {
        for (int i = 0; i < size; i++) {
            array[i] = null;
        }
        size = 0;
    }

    @Override
    public void add(int index, T element) {
        rangeCheckForAdd(index);
        ensureCapacity(size + 1);
        for (int i = size; i > index; i--) {
            array[i] = array[i - 1];
        }
        array[index] = element;
        size++;
    }

    /**
     * 获取index位置的元素
     *
     * @param index
     * @return
     */
    @Override
    public T get(int index) {
        rangeCheck(index);
        return array[index];
    }

    /**
     * 设置index位置的元素
     *
     * @param index
     * @param element
     * @return 原来的元素ֵ
     */
    @Override
    public T set(int index, T element) {
        rangeCheck(index);
        T t = array[index];
        array[index] = element;
        return t;
    }

    @Override
    public T remove(int index) {
        rangeCheck(index);
        T oldElement = array [index];
        for (int i = index; i < size; i++) {
              array[i]=array[i+1];
        }
         array[--size]=null;
        return oldElement;
    }

    /**
     * 查看元素的索引
     * @param t
     * @return
     */
    @Override
    public int indexOf(T t) {
        if(t==null){
            for (int i = 0; i < size; i++) {
                if(array[i]==null){
                    return i;
                }
            }
        }else {
            for (int i = 0; i < size; i++) {
                 if(t.equals(array [i])){
                     return i;
                 }
            }
        }
        return List.ELEMENT_NOT_FOUND;
    }

    /**
     * 确实有可用的容量
     *
     * @param capacity
     */
    private void ensureCapacity(int capacity) {
        int oldSize = array.length;
        if (oldSize >= capacity) {
            return;
        }
        int newCapacity = oldSize + (oldSize >> 1); // >>位移1 相当于除以2.
        T[] newArray = (T[]) new Object[newCapacity];
        for (int i = 0; i < size; i++) {
            newArray[i] = array[i];
        }
        array = newArray;
        System.out.println(oldSize + "扩容为" + newCapacity);
    }

    @Override
    public String toString() {
        StringBuilder string = new StringBuilder();
        string.append("size=").append(size).append(", [");
        for (int i = 0; i < size; i++) {
            if (i != 0) {
                string.append(", ");
            }
            string.append(array[i]);
        }
        string.append("]");
        return string.toString();
    }
}
